let content = document.getElementById("content");
let cartCheckBox = document.getElementsByName("cartCheckBox"); //多选框
let products = document.getElementsByClassName("products"); //每一个商品
let cart_div_7 = document.getElementsByClassName("cart_div_7"); //小计
let yellow = document.getElementsByClassName("yellow")[0]; //总价
let cart_div_4 = document.getElementsByClassName("cart_div_4"); //商品积分
let num_input = document.getElementsByClassName("num_input"); //商品数量
let integral = document.getElementById("integral"); //总积分

// 计算小计、总价、总积分的功能函数
function priceNum(that, num) {
    let price = that.parentNode.previousElementSibling.innerText * 1; //单价
    // 小计=数量*单价
    that.parentNode.nextElementSibling.innerText = num * price;
    // 商品总价=所有小计相加
    let sum = 0;
    for (let i = 0; i < cart_div_7.length; i++) {
        sum += cart_div_7[i].innerText * 1
    }
    yellow.innerText = sum;
    // 总积分=商品数量*单个商品积分，累计求和
    let sum2 = 0;
    for (let i = 0; i < cart_div_4.length; i++) {
        sum2 += cart_div_4[i].innerText * 1 * num_input[i].value;
    }
    integral.innerText = sum2;
}
content.onclick = function () {
    
    // 1.点击全选按钮
    if (event.target.id === "allCheckBox") {
        if (allCheckBox.checked) {
            for (let i = 0; i < cartCheckBox.length; i++) {
                cartCheckBox[i].checked = true;
            }

        } else {
            for (let i = 0; i < cartCheckBox.length; i++) {
                cartCheckBox[i].checked = false;
            }
        }
    }
    // 2.删除所选
    if (event.target.id === "deleteChecked") {
        for (let i = 0; i < cartCheckBox.length; i++) {
            console.log(cartCheckBox[i].checked);
            if (cartCheckBox[i].checked) {
                products[i].remove();
                i--;

            }
        }
    }

    // 3.增加商品
    if (event.target.className === "hand") {
        let num = event.target.previousElementSibling.value++; //数量增加
        let that = event.target;
        priceNum(that, num);


    }

    // 4.减少商品
    if (event.target.className === "hand minus") {
        if (event.target.nextElementSibling.value == 1) {
            alert("商品数量不能小于1");
        } else {
            let num = event.target.nextElementSibling.value--; //数量减少
            let that = event.target;
            priceNum(that, num);
        }



    }

    // 5.删除单个商品
    if(event.target.localName==="a"){
        event.target.parentNode.parentNode.parentNode.remove();
    }

}